// dispatcher测试
#include "dispatcher.hpp"
// 客户端服务器通信测试
#include "message.hpp"
#include <thread>

// 初步实现dispatcher的去类型化
void onRpcResponse(const rcrpc::BaseConnection::ptr& conn,rcrpc::RpcResponse::ptr& msg)
{
    std::cout << "收到了Rpc响应:";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}

void onTopicResponse(const rcrpc::BaseConnection::ptr& conn,rcrpc::TopicResponse::ptr& msg)
{
    std::cout << "收到了Topic响应:";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}


int main()
{
    auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
    dispatcher->registerHandler<rcrpc::RpcResponse>(rcrpc::MType::RSP_RPC, onRpcResponse); // 注册映射关系
    dispatcher->registerHandler<rcrpc::TopicResponse>(rcrpc::MType::RSP_TOPIC, onTopicResponse);

    auto client = rcrpc::ClientFactory::create("127.0.0.1",9090);
     auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
        std::placeholders::_1, std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();  // 具体内容由muduo实现
    auto rpc_req = rcrpc::MessageFactory::create<rcrpc::RpcRequest>();
    rpc_req->setId("11111");
    rpc_req->setMType(rcrpc::MType::REQ_RPC);
    rpc_req->setMethod("ADD");
    Json::Value params;
    params["num1"] = 11;
    params["num2"] = 22;
    rpc_req->setParams(params);
    client->send(rpc_req);

    auto rpc_topic = rcrpc::MessageFactory::create<rcrpc::TopicRequest>();
    rpc_topic->setId("222");
    rpc_topic->setMType(rcrpc::MType::REQ_TOPIC);
    rpc_topic->setOptype(rcrpc::TopicOptype::TOPIC_CREATE);
    rpc_topic->setTopicKey("news");
    client->send(rpc_topic);
    std::this_thread::sleep_for(std::chrono::seconds(10));
    client->shutdown();
    return 0;
}


// // 初步实现dispatcher
// void onRpcResponse(const rcrpc::BaseConnection::ptr& conn,rcrpc::BaseMessage::ptr& msg)
// {
//     std::cout << "收到了Rpc响应:";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }

// void onTopicResponse(const rcrpc::BaseConnection::ptr& conn,rcrpc::BaseMessage::ptr& msg)
// {
//     std::cout << "收到了Topic响应:";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }


// int main()
// {
//     auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
//     dispatcher->registerHandler(rcrpc::MType::RSP_RPC, onRpcResponse); // 注册映射关系
//     dispatcher->registerHandler(rcrpc::MType::RSP_TOPIC, onTopicResponse);

//     auto client = rcrpc::ClinetFactory::create("127.0.0.1",9090);
//      auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
//         std::placeholders::_1, std::placeholders::_2);
//     client->setMessageCallback(message_cb);
//     client->connect();  // 具体内容由muduo实现
//     auto rpc_req = rcrpc::MessageFactory::create<rcrpc::RpcRequest>();
//     rpc_req->setId("11111");
//     rpc_req->setMtype(rcrpc::MType::REQ_RPC);
//     rpc_req->setMethod("ADD");
//     Json::Value params;
//     params["num1"] = 11;
//     params["num2"] = 22;
//     rpc_req->setParams(params);
//     client->send(rpc_req);

//     auto rpc_topic = rcrpc::MessageFactory::create<rcrpc::TopicRequest>();
//     rpc_topic->setId("222");
//     rpc_topic->setMtype(rcrpc::MType::REQ_TOPIC);
//     rpc_topic->setOptype(rcrpc::TopicOptype::TOPIC_CREATE);
//     rpc_topic->setTopicKey("news");
//     client->send(rpc_topic);
//     std::this_thread::sleep_for(std::chrono::seconds(10));
//     client->shutdown();
//     return 0;
// }



// void omMessage(const rcrpc::BaseConnection::ptr& conn,rcrpc::BaseMessage::ptr& msg){
//     std::string body = msg->serialize();
//     std::cout << "recv msg: " << body << std::endl;
// }
// int main()
// {
//     auto client = rcrpc::ClinetFactory::create("127.0.0.1",9090);
//     client->setMessageCallback(omMessage);
//     client->connect();  // 具体内容由muduo实现
//     auto rpc_req = rcrpc::MessageFactory::create<rcrpc::RpcRequest>();
//     rpc_req->setId("11111");
//     rpc_req->setMtype(rcrpc::MType::RSP_RPC);
//     rpc_req->setMethod("ADD");
//     Json::Value params;
//     params["num1"] = 1;
//     params["num2"] = 2;
//     rpc_req->setParams(params);
//     client->send(rpc_req);
//     std::this_thread::sleep_for(std::chrono::seconds(10));
//     client->shutdown();
//     return 0;
// }
